use "transactions.dta", clear

gen lnpic = ln(picnumtotal+1)
rename sizechoice sizechoicenum
gen lnsize = ln(sizechoicenum)
gen lnitem = ln(itemnum)
gen lnword = ln(wordnum+1)
gen decoration = 1 if itemdecoration!=""
replace decoration = 0 if strpos(itemdecoration,"None")
replace decoration=0 if itemdecoration==""
tempfile tempquality
local qualityvar "materialquality fguapro freturnpolicy lnsize lnitem lnpic sellerid productrating"
keep productid `qualityvar'
duplicates drop productid, force
save `tempquality'

set more off
use "transactions.dta", clear
tempfile tempratec tempstar
bys buyercountry: egen crev = sum(transactionvalue)
egen srev = sum(transactionvalue)
gen crate = crev/srev
replace buyercountry = "other" if crate<0.001 //55 out of 180 country
keep if  buyerrating!=.
bys productid: egen cnum = nvals(buyercountry) 
duplicates drop productid, force
 //keep if cnum>=2 //6193 out of 11212 products
keep productid
save `tempratec'

use "`path'short_singlepage_transactions_ship_nodup_201401-201501.dta", clear
merge m:1 productid using `tempratec'
keep if _merge==3
drop _merge
bys buyercountry: egen crev = sum(transactionvalue)
egen srev = sum(transactionvalue)
gen crate = crev/srev
replace buyercountry = "other" if crate<0.001

gen month = month(dofc(time))
gen year = year(dofc(time))
replace month = (year - 2014)*12 + month
gen tranprice = transactionvalue/quantityinpiece
gsort +productid +month +time
by productid month: gen first = _n==1
gen wprice = tranprice if first==1
bys productid month: egen wtranprice = mean(wprice)
gen lnwtranprice = ln(wtranprice)

bys productid month buyercountry: egen pwcquan = sum(quantityinpiece)
bys productid month buyercountry: egen pwcrev = sum(transactionvalue)
bys productid month buyercountry: egen pwcbnum = nvals(buyerid)
gen pwcqb = pwcquan/pwcbnum
foreach i of numlist 1/5 {
	gen star`i' = 1 if buyerrating==`i'
	bys productid month buyercountry: egen star`i'pc = sum(star`i')
	bys productid month: egen star`i'p = sum(star`i')
	}
keep productid month buyercountry star* pw* prevordernum lnwtranprice shipfee*
preserve
foreach i of numlist 1/5 {
	by productid month: egen sumstar`i' = sum(star`i')
	}
duplicates drop productid month, force
bys productid: egen entrymth = min(month)
replace entrymth = 2 if prevordernum!=0
xtset productid month
tsfill, full
bys productid: egen entrymthn = mean(entrymth)
drop if month<entrymth
replace lnwtranprice = l.lnwtranprice if lnwtranprice==.
gen llnwtranprice = l.lnwtranprice
foreach i of numlist 1/5 {
	gen sixstar`i' = 0
	foreach j of numlist 1/6 {
		gen l`j'sumstar`i' = l`j'.sumstar`i'
		replace l`j'sumstar`i' = 0 if l`j'sumstar`i'==.
		replace sixstar`i' = sixstar`i'+l`j'sumstar`i'
		}
	}
keep productid month sixstar* lnwtranprice shipfee*
save `tempstar'
restore

duplicates drop productid month buyercountry, force
merge m:1 productid month using `tempstar'
drop _merge

bys productid: egen entrymth = min(month)
replace entrymth = 2 if prevordernum!=0
keep productid buyercountry month pw* entrymth sixstar* star*pc lnwtranprice shipfee*
fillin productid buyercountry
egen pcgroup = group(productid buyercountry)
replace month = 2 if month ==.
xtset pcgroup month
tsfill, full
gsort +pcgroup -productid -buyercountry
by pcgroup: replace productid = productid[1] if productid ==.
by pcgroup: replace buyercountry = buyercountry[1] if buyercountry == ""
bys productid: egen entrymthnew = min(entrymth)
drop if month<entrymthnew
local mainvar pwcquan pwcrev pwcqb pwcbnum
foreach k of local mainvar {
	replace `k' = 0 if `k' ==.
	gen ln`k' = ln(`k'+1)
}
local qualityvar "materialquality fguapro freturnpolicy lnsize lnitem lnpic sellerid productrating"
capture drop `qualityvar'
merge m:1 productid using `tempquality'
drop if _merge==2 
drop _merge

merge m:1 buyercountry using "distcodegdp.dta"
drop if _merge==2 
drop _merge

merge m:1 buyercountry using "chnmigrants2015.dta"
drop if _merge==2 
drop _merge

merge m:1 productid month using "RDD_treated dummy by productid month.dta"
drop if _merge==2 
drop _merge

merge m:1 productid month using "RDD_true rating by productid month.dta"
drop if _merge==2 
drop _merge

gen treated_lndist=treated*ln(distw)
gen treated_shipfee=treated*ln(shipfee+0.1)
gen treated_contig=treated*contig
gen treated_comlang=treated*comlang_ethno
gen treated_chnmigshare=treated*(chnmigrants2015/(pop_d*1000000))
replace treated_lndist=0 if treated==0
replace treated_shipfee=0 if treated==0
replace treated_contig=0 if treated==0
replace treated_comlang=0 if treated==0
replace treated_chnmigshare=0 if treated==0
tab buyercountry, gen (countrydum)

set more off
eststo quan_gravity1: qui areg lnpwcquan L.lnpwcquan lnwtranprice1 treated treated_lndist truerating materialquality fguapro i.freturnpolicy lnsize lnitem lnpic i.month countrydum*, absorb(sellerid) vce(cl productid)
eststo quan_gravity2: qui areg lnpwcquan L.lnpwcquan lnwtranprice1 treated treated_lndist treated_shipfee truerating materialquality fguapro i.freturnpolicy lnsize lnitem lnpic i.month countrydum*, absorb(sellerid) vce(cl productid)
eststo quan_gravity3: qui areg lnpwcquan L.lnpwcquan lnwtranprice1 treated treated_lndist treated_shipfee treated_comlang truerating materialquality fguapro i.freturnpolicy lnsize lnitem lnpic i.month countrydum*, absorb(sellerid) vce(cl productid)
eststo quan_gravity4: qui areg lnpwcquan L.lnpwcquan lnwtranprice1 treated treated_lndist treated_shipfee treated_chnmigshare truerating materialquality fguapro i.freturnpolicy lnsize lnitem lnpic i.month countrydum*, absorb(sellerid) vce(cl productid)
esttab quan_gravity*, drop(*.month countrydum*) star(* 0.1 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par))  stats(r2 N) mti
